package com.nile;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.ToString;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

/**
 * @Author: zhang kai
 * @Date: 2023/05/29
 * @Description:
 */
public class Test5 {
    public static void main(String[] args) {
        List<TreeNode<String>> nodeList = CollUtil.newArrayList();

        nodeList.add(new TreeNode<>("1", "0", "系统管理", 5));
        nodeList.add(new TreeNode<>("11", "1", "用户管理", 222222));
        nodeList.add(new TreeNode<>("111", "11", "用户添加", 0));
        nodeList.add(new TreeNode<>("2", "0", "店铺管理", 1));
        nodeList.add(new TreeNode<>("21", "2", "商品管理", 44));
        nodeList.add(new TreeNode<>("221", "2", "商品管理2", 2));
        List<Tree<String>> treeList = TreeUtil.build(nodeList, "0");
        System.out.println(treeList);


        List<MenuVo> menuVos = new ArrayList<>();
        menuVos.add(new MenuVo(1,0,"系统管理",1,null));
        menuVos.add(new MenuVo(11,1,"用户管理",10,null));
        menuVos.add(new MenuVo(111,11,"用户添加",9,null));
        menuVos.add(new MenuVo(1111,111,"用户ces1",9,null));
        menuVos.add(new MenuVo(1112,111,"用户ces2",10,null));
        menuVos.add(new MenuVo(1113,111,"用户ces3",8,null));
        menuVos.add(new MenuVo(2,0,"店铺管理",1,null));
        menuVos.add(new MenuVo(22,2,"商品管理",19,null));
        menuVos.add(new MenuVo(222,22,"商品管理",29,null));

        Map<Integer,List<MenuVo>> map = menuVos.stream().collect(Collectors.groupingBy(MenuVo::getParentId));
        map.entrySet().forEach((longListEntry -> Collections.sort(longListEntry.getValue())));
        for(MenuVo vo : menuVos) {
            vo.setChild(map.getOrDefault(vo.getId(),new ArrayList<>()));
        }

        System.out.println(map.get(0));
    }




}
@AllArgsConstructor
@ToString
@Data
class Menu {
    Integer id;

    Integer parentId;

    String name;

    Integer sort;
}
@AllArgsConstructor
@ToString
@Data
class MenuVo implements Comparable<MenuVo> {
    Integer id;

    Integer parentId;

    String name;

    Integer sort;

    List<MenuVo> child;

    @Override
    public int compareTo(MenuVo o) {
        return Integer.compare(this.getSort(),o.getSort());
    }
}